package o;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Images.Thumbnails;
import android.provider.MediaStore.Video;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.bumptech.glide.Glide;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import o.sg.d;

public class sv implements sg<InputStream> {
    private final sz ˊ;
    private final Uri ˋ;
    private InputStream ˏ;

    static class b implements sw {
        private static final String[] ˋ = new String[]{"_data"};
        private final ContentResolver ˏ;

        b(ContentResolver contentResolver) {
            this.ˏ = contentResolver;
        }

        public Cursor ˋ(Uri uri) {
            String lastPathSegment = uri.getLastPathSegment();
            return this.ˏ.query(Thumbnails.EXTERNAL_CONTENT_URI, ˋ, "kind = 1 AND image_id = ?", new String[]{lastPathSegment}, null);
        }
    }

    static class c implements sw {
        private static final String[] ˏ = new String[]{"_data"};
        private final ContentResolver ॱ;

        c(ContentResolver contentResolver) {
            this.ॱ = contentResolver;
        }

        public Cursor ˋ(Uri uri) {
            String lastPathSegment = uri.getLastPathSegment();
            return this.ॱ.query(Video.Thumbnails.EXTERNAL_CONTENT_URI, ˏ, "kind = 1 AND video_id = ?", new String[]{lastPathSegment}, null);
        }
    }

    public static sv ˏ(Context context, Uri uri) {
        return ˏ(context, uri, new b(context.getContentResolver()));
    }

    public static sv ॱ(Context context, Uri uri) {
        return ˏ(context, uri, new c(context.getContentResolver()));
    }

    private static sv ˏ(Context context, Uri uri, sw swVar) {
        return new sv(uri, new sz(Glide.ˎ(context).ᐝ().ˋ(), swVar, Glide.ˎ(context).ˎ(), context.getContentResolver()));
    }

    @VisibleForTesting
    sv(Uri uri, sz szVar) {
        this.ˋ = uri;
        this.ˊ = szVar;
    }

    public void ˏ(@NonNull rl rlVar, @NonNull d<? super InputStream> dVar) {
        try {
            this.ˏ = ˊ();
            dVar.ॱ(this.ˏ);
        } catch (Throwable e) {
            if (Log.isLoggable("MediaStoreThumbFetcher", 3)) {
                Log.d("MediaStoreThumbFetcher", "Failed to find thumbnail file", e);
            }
            dVar.ˏ(e);
        }
    }

    private InputStream ˊ() throws FileNotFoundException {
        int ॱ;
        InputStream ˊ = this.ˊ.ˊ(this.ˋ);
        if (ˊ != null) {
            ॱ = this.ˊ.ॱ(this.ˋ);
        } else {
            ॱ = -1;
        }
        if (ॱ != -1) {
            return new sp(ˊ, ॱ);
        }
        return ˊ;
    }

    public void ˏ() {
        if (this.ˏ != null) {
            try {
                this.ˏ.close();
            } catch (IOException e) {
            }
        }
    }

    public void ॱ() {
    }

    @NonNull
    public Class<InputStream> ˋ() {
        return InputStream.class;
    }

    @NonNull
    public rz ˎ() {
        return rz.LOCAL;
    }
}
